import 'package:flutter/material.dart';
import 'package:spring_fairy/api/mood_track_api.dart';
import 'package:spring_fairy/model/mood_point_image.dart';
import 'package:spring_fairy/model/mood_track.dart';
import 'package:spring_fairy/model/mood_track_point.dart';
import 'package:spring_fairy/model/store.dart';
import 'package:latlong2/latlong.dart';
import 'package:spring_fairy/util/notify.dart';
import '../model/mood_type.dart';
import '../widget/marked_point_card.dart';

class RecordPointListPage extends StatefulWidget {
  final MoodTrack track;
  final List<MoodTrackPoint> points;

  const RecordPointListPage({super.key,
    required this.track,
    required this.points
  });

  @override
  State<RecordPointListPage> createState() => _RecordPointListPageState();
}

class _RecordPointListPageState extends State<RecordPointListPage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:Text(widget.track.title ?? '为命名轨迹',style: Theme.of(context).textTheme.titleLarge),
      ),
      body: RefreshIndicator(
        onRefresh: _onRefresh,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
             if(widget.points.isEmpty)
               Center(
                 child: Padding(
                   padding: const EdgeInsets.only(top: 32),
                   child: Text("当前轨迹还没有标记点!"),
                 ),
               ),
             Expanded(
              child: ListView.builder(
                itemCount: widget.points.length,
                itemBuilder: (context, index) {
                  final point = widget.points[index];
                  int moodId = point.moodTypeId!;
                  MoodType moodType = Store.moodTypes().firstWhere((e) => e.id == moodId);
                  List<MoodPointImage>? pointImages = Store.getPointImages(point);

                  List<String>? imagePaths;
                  if(pointImages != null) {
                    imagePaths = pointImages.map((e) => e.imageUrl!).toList();
                  }

                  var temp = PointData(
                      name: point.locationName!,
                      location: LatLng(point.latitude!, point.longitude!),
                      time: point.createdAt,
                      mood: moodType.id,
                      content: point.description,
                      imagePaths: imagePaths
                  );

                  return MarkedPointCard(
                    point: temp,
                    onEdit: () => (index){},
                    onDelete: () => _deletePoint(point),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _deletePoint(MoodTrackPoint point) async {
    var result = await MoodTrackAPI().deletePoint(point.id!);
    if(!result) {
      return notify('删除失败!', context);
    }
    setState(() {
      Store.deletePoint(point);
      widget.points.remove(point);
    });
    notify('删除成功!', context);
  }

  Future<void> _onRefresh() async {
    await Store.refreshPoints();
    setState(() {
      widget.points.clear();
      widget.points.addAll(Store.getPoints());
    });
  }
}
